package com.ccy.test;

import com.ccy.strategy.ApplePrice;
import com.ccy.strategy.MongoPrice;
import com.ccy.strategy.StrawberryPrice;

import java.math.BigDecimal;
import java.util.Scanner;

/**
 * @Author ChenChangYu
 * @Date 2022 10 08 15 45
 **/
public class BuyFruit {
    private static Scanner scanner = new Scanner(System.in);

    public static void main(String[] args) {
        System.out.println("请输入以下购买顾客: A,B,C,D");
        System.out.println("输入exit退出");
        String customer = scanner.next();
        loop1:
        while (true) {
            switch (customer) {
                case "A":
                    buyAppleAndStrawberry();
                    break loop1;
                case "B":
                    buyAll();
                    break loop1;
                case "C":
                    buyAllAndDiscount();
                    break loop1;
                case "D":
                    buyAllFullReduce();
                    break;
                case "exit":
                    break loop1;
                default:
                    System.out.println("输入错误,请重新输入顾客:");
                    customer = scanner.next();
                    break;
            }
        }
    }

    /**
     * 满100减20
     */
    private static void buyAllFullReduce() {
        try {
            System.out.println("请输入苹果购买斤数:");
            int appleWeight = Integer.parseInt(scanner.next());
            System.out.println("请输入草莓购买斤数:");
            int strawberryWeight = Integer.parseInt(scanner.next());
            System.out.println("请输入芒果购买斤数:");
            int mongoWeight = Integer.parseInt(scanner.next());
            System.out.println("商品总价为:" + d(appleWeight, strawberryWeight, mongoWeight));
        } catch (Exception e) {
            System.out.println("输入有误");
        }
    }

    /**
     * 草莓打八折购买
     */
    private static void buyAllAndDiscount() {
        try {
            System.out.println("请输入苹果购买斤数:");
            int appleWeight = Integer.parseInt(scanner.next());
            System.out.println("请输入草莓购买斤数:");
            int strawberryWeight = Integer.parseInt(scanner.next());
            System.out.println("请输入芒果购买斤数:");
            int mongoWeight = Integer.parseInt(scanner.next());
            System.out.println("商品总价为:" + c(appleWeight, strawberryWeight, mongoWeight));
        } catch (Exception e) {
            System.out.println("输入有误");
        }
    }

    /**
     * 买三种水果
     */
    private static void buyAll() {
        try {
            System.out.println("请输入苹果购买斤数:");
            int appleWeight = Integer.parseInt(scanner.next());
            System.out.println("请输入草莓购买斤数:");
            int strawberryWeight = Integer.parseInt(scanner.next());
            System.out.println("请输入芒果购买斤数:");
            int mongoWeight = Integer.parseInt(scanner.next());
            System.out.println("商品总价为:" + b(appleWeight, strawberryWeight, mongoWeight));
        } catch (Exception e) {
            System.out.println("输入有误");
        }
    }

    /**
     * A顾客买苹果和草莓
     */
    private static void buyAppleAndStrawberry() {
        try {
            System.out.println("请输入苹果购买斤数:");
            int appleWeight = Integer.parseInt(scanner.next());
            System.out.println("请输入草莓购买斤数:");
            int strawberryWeight = Integer.parseInt(scanner.next());
            System.out.println("商品总价为:" + a(appleWeight, strawberryWeight));
        } catch (Exception e) {
            System.out.println("输入有误");
        }
    }

    /**
     * 满减
     *
     * @param price
     * @return
     */
    public static Double fullReduce(double price) {
        if (price > 100) {
            return new BigDecimal(String.valueOf(price))
                    .subtract(BigDecimal.valueOf((price / 100) * 20))
                    .doubleValue();

        }
        return price;
    }

    /**
     * a顾客购买
     *
     * @param appleWeight
     * @param strawBerryWeight
     * @return
     */
    public static Double a(int appleWeight, int strawBerryWeight) {
        Double applePrice = new ApplePrice().totalPrice(appleWeight);
        Double strawBerryPrice = new StrawberryPrice().totalPrice(strawBerryWeight);
        return new BigDecimal(applePrice).add(new BigDecimal(strawBerryPrice)).doubleValue();
    }


    /**
     * b顾客购买
     *
     * @param appleWeight
     * @param strawBerryWeight
     * @param mongoWeight
     * @return
     */
    public static Double b(int appleWeight, int strawBerryWeight, int mongoWeight) {
        Double totalPrice = new ApplePrice().totalPrice(appleWeight);
        Double strawBerryPrice = new StrawberryPrice().totalPrice(strawBerryWeight);
        Double mongoPrice = new MongoPrice().totalPrice(mongoWeight);
        return new BigDecimal(totalPrice)
                .add(new BigDecimal(strawBerryPrice))
                .add(new BigDecimal(mongoPrice))
                .doubleValue();
    }

    /**
     * c顾客购买
     *
     * @param appleWeight
     * @param strawBerryWeight
     * @param mongoWeight
     * @return
     */
    public static Double c(int appleWeight, int strawBerryWeight, int mongoWeight) {
        Double totalPrice = new ApplePrice().totalPrice(appleWeight);
        Double strawBerryPrice = new StrawberryPrice().discountPrice(strawBerryWeight);  //打折后的价格
        Double mongoPrice = new MongoPrice().totalPrice(mongoWeight);
        return new BigDecimal(totalPrice)
                .add(new BigDecimal(strawBerryPrice))
                .add(new BigDecimal(mongoPrice))
                .doubleValue();
    }

    /**
     * c顾客购买
     *
     * @param appleWeight
     * @param strawBerryWeight
     * @param mongoWeight
     * @return
     */
    public static Double d(int appleWeight, int strawBerryWeight, int mongoWeight) {
        Double totalPrice = new ApplePrice().totalPrice(appleWeight);
        Double strawBerryPrice = new StrawberryPrice().totalPrice(strawBerryWeight);
        Double mongoPrice = new MongoPrice().totalPrice(mongoWeight);
        return fullReduce(new BigDecimal(totalPrice)
                .add(new BigDecimal(strawBerryPrice))
                .add(new BigDecimal(mongoPrice))
                .doubleValue());
    }
}



























